package com.itheima.controller;

import com.itheima.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class controller1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList();
        Lo:
        while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent(list, sc);
                    break;
                case "2":
                    //  System.out.println("删除学生");
                    deleteStudent(list, sc);
                    break;
                case "3":
                    // System.out.println("修改学生");
                    rewrite(list, sc);
                    break;
                case "4":
                    // System.out.println("查看学生");
                    checkStudent(list);
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    break Lo;
                default:
                    System.out.println("您的输入有误,请重新输入");
                    break;
            }
        }
    }

    //修改
    public static void rewrite(ArrayList<Student> list, Scanner sc) {
        System.out.println("请输入您要修改的学号");
        String s = sc.next();
        int index = getIndex(list, s);
        if (index == -1) {
            System.out.println("查无此人，请重新输入");
        } else {
            System.out.println("请输入修改后姓名");
            String s2 = sc.next();
            System.out.println("请输入修改后年龄");
            String s3 = sc.next();
            System.out.println("请输入修改后生日");
            String s4 = sc.next();
           /* Student ss=new Student(s,s2,s3,s4);
            list.set(index,ss);*/
            list.get(index).setName(s2);
            list.get(index).setAge(s3);
            list.get(index).setBirthday(s4);
            System.out.println("修改成功");
        }
    }

    //查询
    public static void checkStudent(ArrayList<Student> list) {
        //判断list是否存在数据
        if (list.size() == 0) {
            System.out.println("暂无信息，请先添加数据");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (Student x : list) {
            System.out.println(x.getStudentNumber() + "\t\t" + x.getName() + "\t" + x.getAge() + "\t" + x.getBirthday());
        }
    }

    //删除学生
    public static void deleteStudent(ArrayList<Student> list, Scanner sc) {
        System.out.println("请输入您要删除的学号");
        String s = sc.next();
        int index = getIndex(list, s);
        if (index == -1) {
            System.out.println("查无此人，请重新输入");
        } else {
            list.remove(index);
            System.out.println("删除成功");
        }
    }

    //获取索引
    public static int getIndex(ArrayList<Student> list, String sb) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);                   //遍历集合，得到每一个学生对象
            if (s.getStudentNumber().equals(sb)) {        //每一个学生对象调用学生的学号与输入的学号比较
                index = i;
                break;
            }
        }
        return index;
    }

    //添加学生
    public static void addStudent(ArrayList<Student> list, Scanner sc) {
        String s1;
        while (true) {
            System.out.println("请输入学号");
            s1 = sc.next();
            int index = getIndex(list, s1);
            if (index == -1) {
                break;
            } else {
                System.out.println("你输入的学号已存在，请重新输入");
            }
        }
        System.out.println("请输入姓名");
        String s2 = sc.next();
        System.out.println("请输入年龄");
        String s3 = sc.next();
        System.out.println("请输入生日");
        String s4 = sc.next();
        Student ss = new Student(s1, s2, s3, s4);
        list.add(ss);
        System.out.println("添加成功");
    }
}
